package com.wangsd.web;

import com.wangsd.common.utils.ApplicationUtils;
import com.wangsd.web.face.entity.Member;
import com.wangsd.web.sys.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

import static com.wangsd.web.miniapp.MiniAppController.LOGIN_TOKEN_TYPE_M;

/**
 * 基础 controller
 */
@Slf4j
public abstract class MyController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected HttpSession session;

    @Autowired
    protected ServletContext application;

    protected SysUser getUserInfo() {
        String token = request.getHeader("Access-Token");
        SysUser user = (SysUser) redisTemplate.opsForValue().get(token);
        return user;
    }

    protected SysUser getUserInfo(String token) {
        SysUser user = (SysUser) redisTemplate.opsForValue().get(token);
        return user;
    }

    protected String getMiniAppid() {
        String referer = request.getHeader("referer");
        String[] arr = referer.split("\\/");
        return arr[3];
    }

    protected Member getMiniUserInfo() {
        String token = request.getHeader("Access-Token");
        if (token == null) {
            return null;
        } else if (!token.contains(LOGIN_TOKEN_TYPE_M)) {
            return null;
        }
        if (LOGIN_TOKEN_TYPE_M.equals(token.substring(0, 1))) {
            Member member = (Member) redisTemplate.opsForValue().get(token);
            return member;
        }
        return null;
    }

    /**
     * 下载文件
     *
     * @param file 文件
     */
    protected ResponseEntity<Resource> download(File file) {
        String fileName = file.getName();
        return download(file, fileName);
    }

    /**
     * 下载
     *
     * @param file     文件
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     */
    protected ResponseEntity<Resource> download(File file, String fileName) {
        Resource resource = new FileSystemResource(file);
        try {
            return ResponseEntity
                    .ok()
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .contentLength(resource.contentLength())
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + ApplicationUtils.encodeURL(fileName))
                    .body(resource);
        } catch (IOException e) {
            log.error("download", e);
        }
        return null;
    }
}
